<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<div id="app">
    <div>
        <h3>Vue表单验证案例</h3>
        <input type="text" v-model="name">
        <input type="password" v-model="password">
        <button v-on:click='verify'>提交</button>
    </div>

</div>
</body>
<script src="../lib/vue.js"></script>
<script>

    new Vue({
        el: '#app',
        data: {
            name: '',
            password: '',
            dis: {
                name_verify: false,
                password_verify: false
            }
        },
        watch: {
            // 监听name输入值的变化
            name: function (val) {
                let name_length = false
                let name_value = false
                // 字符串长度验证
                if (val.length >= 3) {
                    name_length = true
                } else {
                    name_length = false
                }

                if (val === 'zyd') {
                    name_value = true
                } else {
                    name_value = false
                }

                if (name_value && name_length) {
                    this.dis.name_verify = true
                } else {
                    this.dis.name_verify = false
                }
            },
            password: function (val) {
                let password_length = false
                let password_value = false
                // 字符串长度验证
                if (val.length >= 4) {
                    password_length = true
                } else {
                    password_length = false
                }

                if (val === 'zyd123') {
                    password_value = true
                } else {
                    password_value = false
                }

                if (password_length && password_value) {
                    this.dis.password_verify = true
                } else {
                    this.dis.password_verify = false
                }
            }
        },
        methods: {
            verify: function () {
                if (this.dis.name_verify && this.dis.password_verify) {
                    alert('登录成功!!')
                } else {
                    alert('登录失败')
                }
            }
        }
    })

</script>
</html>